const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * scene解码
 */
const scene_decode = (e) => {  //scene: "cid%3A10005%2Cuid%3A10001"
  if (e === undefined)
    return {};
  let scene = decodeURIComponent(e),  //对 encodeURIComponent() 函数编码的 URI 进行解码  scene==> cid:10005,uid:10001
    params = scene.split(','),   
    data = {};
  for (let i in params) {
    var val = params[i].split(':');
    val.length > 0 && val[0] && (data[val[0]] = val[1] || null)
  }
  return data;
}

const get = (p, o) => {
  return p.reduce((xs, x) => (xs && (xs[x] || xs[x] == 0 || xs[x] === false)) ? xs[x] : null, o)
}

module.exports = {
  formatTime: formatTime,
  scene_decode: scene_decode,
  get: get
}
